package com.medical.wechat.web.controller;

import com.medical.common.JsonResult;
import com.medical.wechat.model.WechatTemplate;
import com.medical.wechat.service.WechatTemplateService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/1.
 */
@Controller
@RequestMapping("/wechatTemplate")
public class WechatTemplateController {

    @Autowired
    private WechatTemplateService wechatTemplateService;

    //根据accountId查找模板消息
    @ApiOperation(value="根据accountId查找模板消息", notes="根据accountId查找模板消息")
    @RequestMapping(value = "/get/{accountId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult selectALL(@PathVariable String accountId) {
        List<WechatTemplate> wechatTemplateList = wechatTemplateService.findByAccountId(accountId);

        if (wechatTemplateList.size() == 0) {
            return new JsonResult("200", "", getDefaultTemplate());
        } else {
            return new JsonResult("200", "", wechatTemplateList);
        }
    }

     //新增回复模板
     @ApiOperation(value="新增回复模板", notes="新增回复模板")
    @RequestMapping(value = "/post",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult postTemplate(@RequestParam(value = "id",required = false) String id  ,@RequestParam(value = "tempKey",required = false) String tempKey  ,
                                   @RequestParam(value = "name",required = false) String name  ,@RequestParam(value = "content",required = false) String content  ,
                                   @RequestParam(value = "industry",required = false) String industry  ,@RequestParam(value = "topColor",required = false) String  topColor ,
                                   @RequestParam(value = "textColor",required = false) String textColor  ,@RequestParam(value = "templateMsgId",required = false) String templateMsgId  ,
                                   @RequestParam(value = "status",required = false) String status  ,@RequestParam(value = "accountId",required = false) String  accountId){
        try {
            WechatTemplate wechatTemplate = new WechatTemplate();
            wechatTemplate.setId(id);
            wechatTemplate.setTempKey(tempKey);
            wechatTemplate.setName(name);
            wechatTemplate.setContent(content);
            wechatTemplate.setIndustry(industry);
            wechatTemplate.setTopColor(topColor);
            wechatTemplate.setTextColor(textColor);
            wechatTemplate.setTemplateMsgId(templateMsgId);
            wechatTemplate.setStatus(Integer.valueOf(status));
            wechatTemplate.setAccountId(accountId);
            wechatTemplateService.createWechatTemplate(wechatTemplate);
            return new JsonResult("200", "", "增添成功");
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }


    //编辑
    @ApiOperation(value="编辑", notes="编辑")
    @RequestMapping(value = "/patch/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult patchTemplate(@PathVariable String id,
                                    @RequestParam(value = "tempKey",required = false) String tempKey  ,
                                    @RequestParam(value = "name",required = false) String name  ,@RequestParam(value = "content",required = false) String content  ,
                                    @RequestParam(value = "industry",required = false) String industry  ,@RequestParam(value = "topColor",required = false) String  topColor ,
                                    @RequestParam(value = "textColor",required = false) String textColor  ,@RequestParam(value = "templateMsgId",required = false) String templateMsgId  ,
                                    @RequestParam(value = "status",required = false) String status  ,@RequestParam(value = "accountId",required = false) String  accountId) {
        try {

            WechatTemplate wechatTemplate=wechatTemplateService.selectById(id);
            wechatTemplate.setTempKey(tempKey);
            wechatTemplate.setName(name);
            wechatTemplate.setContent(content);
            wechatTemplate.setIndustry(industry);
            wechatTemplate.setTopColor(topColor);
            wechatTemplate.setTextColor(textColor);
            wechatTemplate.setTemplateMsgId(templateMsgId);
            wechatTemplate.setStatus(Integer.valueOf(status));
            wechatTemplate.setAccountId(accountId);
            wechatTemplateService.updateById(wechatTemplate);
            return new JsonResult("200", "", wechatTemplateService.selectById(id));
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }


    //删除
    @ApiOperation(value="删除", notes="删除")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult deleteTemplate(@PathVariable String id){
        wechatTemplateService.deleteById(id);
        return new JsonResult("200","","删除成功");
    }

    private List<Map<String, String>> getDefaultTemplate(){

        List<Map<String, String>> wechatTemplateMapList = new ArrayList<Map<String, String>>();

        String[][] StringTemps = {
                {"OPENTM207030392", "诊后问卷温馨提醒", "{{first.DATA}}<br />患者名称：{{keyword1.DATA}}<br />医生：{{keyword2.DATA}}<br />时间：{{keyword3.DATA}}<br />{{remark.DATA}}<br />"},
                {"OPENTM203909810", "预约成功通知", "{{first.DATA}}<br />医生：{{keyword1.DATA}}<br />时间：{{keyword2.DATA}}<br />地点：{{keyword3.DATA}}<br />订单：{{keyword4.DATA}}<br />{{remark.DATA}}<br />"},
                {"OPENTM201005549", "随访提醒", "{{first.DATA}}<br />{{first.DATA}}<br />患者姓名：{{keyword1.DATA}}<br />预约时间：{{keyword2.DATA}}<br />提醒事项：{{keyword3.DATA}}<br />医生信息：{{keyword4.DATA}}<br />{{remark.DATA}}<br />"},
                {"OPENTM203909902", "预约取消通知", "{{first.DATA}}<br />医生：{{keyword1.DATA}}<br />时间：{{keyword2.DATA}}<br />地点：{{keyword3.DATA}}<br />订单：{{keyword4.DATA}}<br />{{remark.DATA}}<br />"},
                {"OPENTM203448488", "缴费提醒", "{{first.DATA}}<br />医生：{{keyword1.DATA}}<br />类型：{{keyword2.DATA}}<br />应缴：{{keyword3.DATA}}<br />时间：{{keyword4.DATA}}<br />交易号：{{keyword5.DATA}}<br />{{remark.DATA}}<br />"},
                {"OPENTM206988070", "疾病解析通知", "{{first.DATA}}<br />疾病：{{keyword1.DATA}}<br />医生：{{keyword2.DATA}}<br />地点：{{keyword3.DATA}}<br />时间：{{keyword4.DATA}}<br />{{remark.DATA}}<br />"},
        };

        for(String[] temps : StringTemps){
            Map<String, String> map = new HashMap<String, String>();
            map.put("id", "");
            map.put("tempKey", temps[0]);
            map.put("name", temps[1]);
            map.put("content", temps[2]);
            map.put("industry", "医疗护理|医药医疗");
            map.put("topColor", "#029700");
            map.put("textColor", "#000000");
            map.put("status", "0");
            map.put("templateMsgId", "");
            wechatTemplateMapList.add(map);
        }

        return wechatTemplateMapList;
    }
}
